Imports System.Data.SqlClient

Public Class cls_talonariorecibo_

    Implements ICloneable

#Region "Variables ..."
    Private int_idtalonariorecibo As Integer = 0
    Private int_numero As Integer = 0
    Private int_desde As Integer = 0
    Private int_hasta As Integer = 0
    Private dte_ingreso As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private str_observaciones As String = ""
    Private int_idregistro As Integer = 0
    Private int_ultimo As Integer = 0

    Private obj_registro As cls_registro_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_talonariorecibo_) = Nothing
#End Region

    Public Overrides Function ToString() As String
        Return "Talonario: " & numero & " (Desde: " & desde & " Hasta:" & hasta & ")"
    End Function

#Region "Propiedades ..."
    ReadOnly Property This
        Get
            Return Me
        End Get
    End Property
    ReadOnly Property ThisDescripcion As String
        Get
            Return Me.ToString
        End Get
    End Property
    ReadOnly Property pKey
        Get
            Return idtalonariorecibo
        End Get
    End Property
    Property idtalonariorecibo() As Integer
        Get
            idtalonariorecibo = int_idtalonariorecibo
        End Get
        Set(ByVal value As Integer)
            int_idtalonariorecibo = value
        End Set
    End Property
    Property numero() As Integer
        Get
            numero = int_numero
        End Get
        Set(ByVal value As Integer)
            int_numero = value
        End Set
    End Property
    Property desde() As Integer
        Get
            desde = int_desde
        End Get
        Set(ByVal value As Integer)
            int_desde = value
        End Set
    End Property
    Property hasta() As Integer
        Get
            hasta = int_hasta
        End Get
        Set(ByVal value As Integer)
            int_hasta = value
        End Set
    End Property
    Property ingreso() As DateTime
        Get
            ingreso = dte_ingreso
        End Get
        Set(ByVal value As DateTime)
            dte_ingreso = value
        End Set
    End Property
    Property observaciones() As String
        Get
            observaciones = str_observaciones
        End Get
        Set(ByVal value As String)
            str_observaciones = value
        End Set
    End Property
    Property idregistro() As Integer
        Get
            idregistro = int_idregistro
        End Get
        Set(ByVal value As Integer)
            int_idregistro = value
        End Set
    End Property
    Property ultimo As Integer
        Get
            ultimo = int_ultimo
        End Get
        Set(ByVal value As Integer)
            int_ultimo = value
        End Set
    End Property
    Property registro() As cls_registro_
        Get
            If IsNothing(obj_registro) Then
                obj_registro = New cls_registro_
                If Not obj_registro.Buscar(idregistro) Then
                    obj_registro = Nothing
                End If
            End If
            registro = obj_registro
        End Get
        Set(ByVal value As cls_registro_)
            obj_registro = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_talonariorecibo_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_talonariorecibo_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_talonariorecibo_)
        While obj_Reader.Read()
            Dim Elemento As New cls_talonariorecibo_
            Elemento.int_idtalonariorecibo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtalonariorecibo")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("numero"))) = False Then
                Elemento.int_numero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("numero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("desde"))) = False Then
                Elemento.int_desde = (obj_Reader.GetValue(obj_Reader.GetOrdinal("desde")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("hasta"))) = False Then
                Elemento.int_hasta = (obj_Reader.GetValue(obj_Reader.GetOrdinal("hasta")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ingreso"))) = False Then
                Elemento.dte_ingreso = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ingreso")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones"))) = False Then
                Elemento.str_observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
                Elemento.int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ultimo"))) = False Then
                Elemento.int_ultimo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ultimo")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idtalonariorecibo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idtalonariorecibo")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("numero"))) = False Then
            int_numero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("numero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("desde"))) = False Then
            int_desde = (obj_Reader.GetValue(obj_Reader.GetOrdinal("desde")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("hasta"))) = False Then
            int_hasta = (obj_Reader.GetValue(obj_Reader.GetOrdinal("hasta")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ingreso"))) = False Then
            dte_ingreso = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ingreso")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones"))) = False Then
            str_observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("observaciones")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro"))) = False Then
            int_idregistro = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idregistro")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("ultimo"))) = False Then
            int_ultimo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("ultimo")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            'Creacion de registro----------------------------------------------------
            If IsNothing(obj_registro) Then
                registro = New cls_registro_
                registro.fechacreado = Now
                registro.idusuariocreado = UsuarioLOG.idusuario
                registro.fechamodificado = Now
                registro.idusuariomodificado = UsuarioLOG.idusuario
                registro.idregistroestado = 1
                registro.idusuarioregistroestado = UsuarioLOG.idusuario
                registro.Nuevo(True)
                idregistro = obj_registro.idregistro
            End If
            '------------------------------------------------------------------------
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "talonariorecibo__Nuevo"
                .Parameters.Add("@numero", SqlDbType.Int).Value = int_numero
                .Parameters.Add("@desde", SqlDbType.Int).Value = int_desde
                .Parameters.Add("@hasta", SqlDbType.Int).Value = int_hasta
                .Parameters.Add("@ingreso", SqlDbType.DateTime).Value = dte_ingreso
                .Parameters.Add("@observaciones", SqlDbType.NVarChar).Value = str_observaciones
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@ultimo", SqlDbType.Int).Value = int_ultimo
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de talonariorecibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            'Modificacion de registro------------------------------------------------
            registro.fechamodificado = Now : registro.idusuariomodificado = UsuarioLOG.idusuario
            registro.Modificar()
            '------------------------------------------------------------------------
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "talonariorecibo__Modificar"
                .Parameters.Add("@idtalonariorecibo", SqlDbType.Int).Value = int_idtalonariorecibo
                .Parameters.Add("@numero", SqlDbType.Int).Value = int_numero
                .Parameters.Add("@desde", SqlDbType.Int).Value = int_desde
                .Parameters.Add("@hasta", SqlDbType.Int).Value = int_hasta
                .Parameters.Add("@ingreso", SqlDbType.DateTime).Value = dte_ingreso
                .Parameters.Add("@observaciones", SqlDbType.NVarChar).Value = str_observaciones
                .Parameters.Add("@idregistro", SqlDbType.Int).Value = int_idregistro
                .Parameters.Add("@ultimo", SqlDbType.Int).Value = int_ultimo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de talonariorecibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idtalonariorecibo As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "talonariorecibo__Eliminar"
                .Parameters.Add("@idtalonariorecibo", SqlDbType.Int).Value = idtalonariorecibo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de talonariorecibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idtalonariorecibo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "talonariorecibo__Buscar"
                .Parameters.Add("@idtalonariorecibo", SqlDbType.Int).Value = idtalonariorecibo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de talonariorecibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "talonariorecibo__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de talonariorecibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos(Optional ByVal idregistroestado As Integer = 1) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "talonariorecibo__CargarTodos"
                .Parameters.Add("@idregistroestado", SqlDbType.Int).Value = idregistroestado
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de talonariorecibo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_talonariorecibo_Clon As New cls_talonariorecibo_
        obj_talonariorecibo_Clon.int_idtalonariorecibo = Me.int_idtalonariorecibo
        obj_talonariorecibo_Clon.int_numero = Me.int_numero
        obj_talonariorecibo_Clon.int_desde = Me.int_desde
        obj_talonariorecibo_Clon.int_hasta = Me.int_hasta
        obj_talonariorecibo_Clon.dte_ingreso = Me.dte_ingreso
        obj_talonariorecibo_Clon.str_observaciones = Me.str_observaciones
        obj_talonariorecibo_Clon.int_idregistro = Me.int_idregistro
        obj_talonariorecibo_Clon.int_ultimo = Me.int_ultimo
        obj_talonariorecibo_Clon.col_Items = Me.col_Items
        Return obj_talonariorecibo_Clon
    End Function

End Class
